package com.example.springboottest.student.service.impl;

import com.example.springboottest.student.dao.StuJpaDao;
import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.service.StuJpa2Service;
import com.example.springboottest.student.service.StuJpaService;
import com.example.springboottest.student.service.TempService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service("stuJpaService")
//@ConfigurationProperties(prefix = "self.constant")
public class StuJpaServiceImpl implements StuJpaService {
    @Autowired
    private StuJpaDao stuJpaDao;
    @Autowired
    private StuJpa2Service stuJpa2Service;
    @Autowired
    private TempService tempService;

    private String selfTestString;

    public void setSelfTestString(String selfTestString) {
        this.selfTestString = selfTestString;
    }

    List<Lock> locks = new ArrayList<Lock>();

    Lock lock = new ReentrantLock(true);

    Random ra = new Random();

    private int id = 1;

    public StuJpaServiceImpl() {
        for (int i = 0; i < 5; i++) {
            locks.add(new ReentrantLock(true));
        }
    }

    @Override
    public List<Student> doFindALl() {
        System.out.println(selfTestString);
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        return stuJpaDao.findAll(sort);
    }

    @Override
    public void doDeleteById(String id) {

    }

    @Override
    public void doCreate(Student student) {
        stuJpaDao.save(student);
    }

    @Override
    public void doUpdate(Student student) {
        try {
            System.out.println("1111");
            int i = 9 / 0;
            stuJpaDao.save(student);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("2222");
            int i = 9 / 0;
            System.out.println("3333");
        }
    }

    @Override
    public void doUpdateLock(Student student) {
        tempService.doGetdOracleSessionId();
//        tempService.doReadTempBySession();
        tempService.doReadTempByTranscation();
//        stuJpaDao.save(student);
        Student _student = doFindById(student.getId());
        _student.setAddress("1111111111");
//        Student newStudent = new Student();
//        newStudent.setId("3");
//        newStudent.setAge(333);
//        stuJpaDao.save(newStudent);
        stuJpaDao.save(_student);
        try {
//            if (StringUtils.equals(student.getId(), "1"))
//            Thread.sleep(30 * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        tempService.doCreateTempBySession();
        tempService.doCreateTempByTranscation();
        stuJpaDao.flush();
        stuJpa2Service.idoUpdate(_student);
        tempService.doReadTempByTranscation();
//        tempService.doReadTempBySession();
//        Student _student2 = doFindById(student.getId());
//        System.out.println("_student2 == _student:\t" + (_student2 == student));
//        _student2.setAge(888);
//        stuJpaDao.save(_student2);
        System.out.println("age:\t" + _student.getAge());
    }

    @Override
    public void idoUpdate(Student student) {

    }

    @Override
    public Student doFindById(String id) {
        Student student = stuJpaDao.findById(id).orElse(null);
        return student;
    }

    @Override
    public Page<Student> doPage() {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(1, 2, sort);
        return stuJpaDao.findAll(pageable);
    }

    @Override
    public String doLock() {
        int index = ra.nextInt(5);
        System.out.println(Thread.currentThread().getName() + "\t等待获取 lock-" + index);
        locks.get(index).lock();
//        lock.lock();
        System.out.println(Thread.currentThread().getName() + "\t得到了 lock-" + index + " 休眠1分钟");
        try {
            for (int i = 0; i < 60; i++) {
                stuJpaDao.findById("" + id++);
                stuJpaDao.findById("" + id++);
                Thread.sleep(1 * 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            locks.get(index).unlock();
//            lock.unlock();
        }
        System.out.println(Thread.currentThread().getName() + "\t释放了 lock-" + index);
        return "stop";
    }
}